
#ifndef __NEBULA_UTILITY_STREAMSTLSTREAMADAPTOR__INCLUDE__
#define __NEBULA_UTILITY_STREAMSTLSTREAMADAPTOR__INCLUDE__

#include <iostream>
#include <vector>

namespace nebula {

namespace sdk
{
	class IOStream;
	class IIStream;
}

namespace utility {

using ::nebula::sdk::IOStream;
using ::nebula::sdk::IIStream;

//////////////////////////////////////////////////////////////////////////
//IOStream:
class OStreamAdaptorBuf: public std::streambuf
{
public:
	explicit OStreamAdaptorBuf(IOStream *stream);
	~OStreamAdaptorBuf();

	void close();

protected:
	virtual int sync();
	virtual int_type overflow(int_type c);

private:
	IOStream *_stream;
	std::vector<char> _buffer;
};

class OStreamAdaptor: public std::ostream
{
public:
	explicit OStreamAdaptor(IOStream *stream);
	~OStreamAdaptor();

	void close();

private:
	// copy constructor not implemented
	OStreamAdaptor(const OStreamAdaptor &);
	OStreamAdaptor& operator= (const OStreamAdaptor &);

private:
	OStreamAdaptorBuf m_buff;
};

//////////////////////////////////////////////////////////////////////////
//IIStream:
class IStreamAdaptorBuf : public std::streambuf
{
public:
	explicit IStreamAdaptorBuf(IIStream* stream);
	~IStreamAdaptorBuf();

	void close();

protected:
	int_type underflow();

private:
	IIStream* _stream;
	std::vector<char> _buffer;
};

class IStreamAdaptor: public std::istream
{
public:
	explicit IStreamAdaptor(IIStream *stream);
	~IStreamAdaptor();

	void close();

private:
	// copy constructor not implemented
	IStreamAdaptor(const IStreamAdaptor &);
	IStreamAdaptor& operator= (const IStreamAdaptor &);

private:
	IStreamAdaptorBuf m_buff;
};

}} // end of nebula::utility

#endif //end of __NEBULA_UTILITY_STREAMSTLSTREAMADAPTOR__INCLUDE__

